import psutil


def get_process_info():
    # 获取所有进程
    processes = []
    for proc in psutil.process_iter(['pid', 'name', 'cpu_percent', 'memory_info']):
        try:
            process_info = proc.info
            if str(process_info['name']).startswith("AIBOX-") or process_info["pid"] == 55916:
                print(process_info['name'])
                continue
            else:
                continue
            # 获取进程的详细信息
            # process_info = proc.info
            # 计算内存使用量（MB）
            if process_info['memory_info']:
                memory_usage_mb = process_info['memory_info'].rss / (1024 * 1024)
            else:
                continue
            # 将信息添加到列表中
            processes.append({
                'pid': process_info['pid'],
                'name': process_info['name'],
                'cpu_percent': process_info['cpu_percent'],
                'memory_usage_mb': memory_usage_mb
            })
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            # 忽略不存在的进程、访问被拒绝的进程和僵尸进程
            pass
    return processes


if __name__ == "__main__":
    process_info_list = get_process_info()
    for process in process_info_list:
        print(f"PID: {process['pid']}, Name: {process['name']}, "
              f"CPU Usage: {process['cpu_percent']}%, Memory Usage: {process['memory_usage_mb']:.2f} MB")
